package info.heidao.ww2.framework.config;

import io.undertow.server.DefaultByteBufferPool;
import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: zhengheng
 * @Date: 2020-07-22 19:40
 * @Description:
 */
@Configuration
public class WebFrameWorkAutoConfigure {

    @ConditionalOnClass(WebServerFactoryCustomizer.class)
    @Bean
    public WebServerFactory webServerFactory() {
        return new WebServerFactory();
    }


    private static class WebServerFactory implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {

        @Override
        public void customize(UndertowServletWebServerFactory factory) {
            factory.addDeploymentInfoCustomizers(deploymentInfo -> {
                WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo();
                webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 8192));
                deploymentInfo.addServletContextAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME, webSocketDeploymentInfo);
            });
        }
    }
}
